//////////////////////////////////////////////////////////////////
// MattiseOS Kernel
// Date: 2008
// Author: Matthew Iselin
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//////////////////////////////////////////////////////////////////

#ifndef VFS_H
#define VFS_H

// Includes
#include <sys/stat.h>
#include <devman/devman.h>

// Defines

// the maximum size for the prefix and device paths
#define MAXPREFIXSIZE	64
#define MAXDEVICESIZE	512

// maximum number of entries in the mount/descriptor tables
#define MAXMOUNTENTRIES	1024
#define MAXDESCENTRIES	1024

// if 1, will use a linked list instead of a static array
#define VFS_USE_LINKED_LISTS 0

// Types

// a vfs node
struct vfs_node {
	char prefix[MAXPREFIXSIZE]; // eg "/etc/" (what the user sees)
	char device[MAXDEVICESIZE]; // eg "/hd0/" (what's passed to open)
	int mounted; // whether this vfs node is valid or not
	int perms; // this and following not applicable in 3.0 (for future expansion)
	uint32_t rsvd1;
	uint32_t rsvd2;

#if VFS_USE_LINKED_LISTS
	struct vfs_node* next;
	struct vfs_node* prev;
#endif
};

// a descriptor in the vfs
struct vfs_desc {
	struct vfs_node* node; // pointer to the relevant node (for mount info et al)
	int fd; // the descriptor of the file that's open and being used
	int used; // valid descriptor?

#if VFS_USE_LINKED_LISTS
	struct vfs_node* next;
	struct vfs_node* prev;
#endif
};

// Prototypes

// sets up the VFS for use by anything
void kInitializeVFS();

// mounts a new filesystem in the VFS - maps dir onto device
int vfsmount( int persistent /* MUST BE ZERO IN 3.0 */, const char* device, const char* dir );

// unmounts a filesystem in the VFS
int vfsunmount( const char* dir );

// file i/o stuff
int vfs_open( const char* fn, int mode );
int vfs_read( int fd, const char* buff, size_t len );
int vfs_write( int fd, char* buff, size_t len );
int vfs_seek( int fd, size_t off, int32_t set );
int vfs_stat( int fd, struct stat* buff );
int vfs_ioctl( int fd, int action, int data );
int vfs_close( int fd );

// cleans up the VFS (and writes any changes to the mounttab - NOT IN 3.0)
int kCleanUpVFS();

#endif
